package net.yambal.android.infomation.weather.db;

import java.util.ArrayList;
import java.util.List;

import net.yambal.android.geo.fatAddress;
import net.yambal.android.infomation.weather.Weather;
import net.yambal.android.util.DateUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TableWeather {
	private DBHelperWeather _db_helper;
	private SQLiteDatabase _db;
	public static final String TABLE = "weathers";
	
	public TableWeather(Context context){
		_db_helper = DBHelperWeather.getInstance(context.getApplicationContext());
	}
	
	public Weather getWeather(fatAddress address, Long target_mill){
		if(address == null || target_mill < 0L){
			return null;
		}
		
		List<Weather> weathers = list("location_label = '" + address.search_key + "' AND mills == " + DateUtil.getDateStartMill(target_mill), "1");
		
		if(weathers == null || weathers.size() == 0){
			return null;
		}
		
		return weathers.remove(0);
	}
	
	public void marge(List<Weather> weathers){
		if(weathers == null || weathers.size() == 0){
			return;
		}
		
		while(weathers.size() > 0){
			Weather weather = weathers.remove(0);
			Weather db_weather = getWeather(weather.mills);
			
			open();

			ContentValues values = new ContentValues();
			values.put("mills", weather.mills);
			values.put("location_label", weather.location_label);
			values.put("txt", weather.txt);
			values.put("highest_temperature", weather.highest_temperature);
			values.put("minimum_temperature", weather.minimum_temperature);
			values.put("source", weather.source);
			
			
			if(db_weather != null){
				// update
				_db.update(TABLE, values, "_id = " + db_weather._id, null);
			}else{
				// insert
				_db.insert(TABLE, null, values);
			}
			
			close();
		}
	}
	
	public Weather getWeather(Long target_mill){
		List<Weather> weathers = list("mills = " + target_mill , "1");
		if(weathers == null || weathers.size() == 0){
			return null;
		}
		
		return weathers.remove(0);
	}
	
	public List<Weather> list(String where, String limit){
		List<Weather> weathers = new ArrayList<Weather>();
		open();
		
		Cursor cursor;
		cursor = _db.query(TABLE, new String[] {"_id","mills","location_label","txt","highest_temperature","minimum_temperature","source"}, where, null, null, null, null, limit);
	
		int numRows = 0;
		if (cursor != null && cursor.moveToFirst()) {
			numRows = cursor.getCount();

			for(int i = 0; i<numRows; i++){
				Weather weather = new Weather();
				weather._id = cursor.getLong(0);
				weather.mills = cursor.getLong(1);
				weather.location_label = cursor.getString(2);
				weather.txt = cursor.getString(3);
				weather.highest_temperature = cursor.getInt(4);
				weather.minimum_temperature = cursor.getInt(5);
				weather.source = cursor.getString(6);
				weathers.add(weather);
				
				cursor.moveToNext();
			}
		}
		
		if (cursor != null){
			cursor.close();
		}
		close();
		
		return weathers;
	}
	
	// ----------------------------------------------
	public void open(){
		if(_db_helper != null){
			_db = _db_helper.getWritableDatabase();
		}else{
			_db = null;
		}
	}
	
	public void close() {
		_db.close();
	}
}
